package com.mc.moring.information.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.BaseFragment;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.bean.AppKycPersonalInformacionConfig;
import com.mc.moring.bean.EmergencyBean;
import com.mc.moring.bean.GetFullUserBean;
import com.mc.moring.bean.SingleSelectBean;
import com.mc.moring.bean.ZiLiPageAddressBean;
import com.mc.moring.bean.ZiLiPageArea;
import com.mc.moring.bean.ZiLiPageCity;
import com.mc.moring.bean.ZiLiPageDistrict;
import com.mc.moring.bean.ZiLiPageUserDataBean;
import com.mc.moring.information.CapitalInforActivity;
import com.mc.moring.information.adapter.EmergencyAdapter;
import com.mc.moring.information.modle.KycModle;
import com.mc.moring.util.GsonUtils;
import com.mc.moring.util.PointMgr;
import com.mc.moring.util.T;
import com.mc.moring.util.ToastUtils;
import com.mc.moring.widge.SingleSelectDialogView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FamilialInformationFragment extends BaseFragment {

    CapitalInforActivity activity;

    NestedScrollView parent;
    TextView tv_to;
    TextView et_commune;
    EditText et_address;
    TextView et_marital_status;
    LinearLayout ll_postal_code;
    TextView tv_postal_code;
    TextView tv_postal_code_err;
    View postal_line;
    TextView et_postal_code;
    LinearLayout ll_region;
    TextView tv_region;
    TextView tv_region_err;
    View region_line;
    FrameLayout fl_region;
    TextView et_region;
    LinearLayout ll_city;
    TextView tv_city;
    TextView tv_city_err;
    View city_line;
    FrameLayout fl_city;
    TextView et_city;
    LinearLayout ll_commune;
    TextView tv_commune;
    FrameLayout fl_commune;
    TextView tv_commune_err;
    View commune_line;
    LinearLayout ll_address;
    TextView tv_address;
    TextView tv_address_err;
    View address_line;
    LinearLayout ll_marital_status;
    TextView tv_marital_status;
    FrameLayout fl_marital_status;
    TextView tv_marital_status_err;
    View status_line;
    LinearLayout ll_emergency_name;
    TextView tv_emergency_name;
    RecyclerView emergency;
    TextView tv_add_tiem;
    View view = null;
    ZiLiPageAddressBean addressBean;
    List<ZiLiPageArea> areaList = new ArrayList<>();
    List<ZiLiPageCity> cityList = new ArrayList<>();
    List<ZiLiPageDistrict> districtList = new ArrayList<>();
    List<EmergencyBean> emergencyCon = new ArrayList<>();
    EmergencyAdapter adapter;

    private CapitalInforActivity fatherActivity;

    public KycModle kycModle = null;

    private AppKycPersonalInformacionConfig.FamilyInfoConfigBean familyInfoConfigBean;

    public interface ActivityListener{
        public void sendValue(String value);
    }

    private ActivityListener activityListener;

    public FamilialInformationFragment(Activity activity) {
        this.activity = (CapitalInforActivity) activity;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        fatherActivity = (CapitalInforActivity) activity;
        activityListener = (ActivityListener) getActivity();
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_familial_information, container, false);
        PointMgr.INSTANCE.sendEvent("cl_family_info_open");
        Log.e("PointMgr.INSTANCE.sendEvent", "cl_family_info_open");
        kycModle = KycModle.Companion.getInstant();
        initView();
        initData();
        return view;
    }

    @Override
    public void initView() {
        parent = view.findViewById(R.id.parent);
        tv_to = view.findViewById(R.id.tv_sub_to);
        et_commune = view.findViewById(R.id.et_commune);
        et_address = view.findViewById(R.id.et_address);
        et_region = view.findViewById(R.id.et_region);
        et_marital_status = view.findViewById(R.id.et_marital_status);
        ll_postal_code = view.findViewById(R.id.ll_postal_code);
        tv_postal_code = view.findViewById(R.id.tv_postal_code);
        tv_postal_code_err = view.findViewById(R.id.tv_postal_code_err);
        postal_line = view.findViewById(R.id.postal_line);
        et_postal_code = view.findViewById(R.id.et_postal_code);

        ll_region = view.findViewById(R.id.ll_region);
        tv_region = view.findViewById(R.id.tv_region);
        fl_region = view.findViewById(R.id.fl_region);
        tv_region_err = view.findViewById(R.id.tv_region_err);
        region_line = view.findViewById(R.id.region_line);

        ll_city = view.findViewById(R.id.ll_city);
        tv_city = view.findViewById(R.id.tv_city);
        tv_city_err = view.findViewById(R.id.tv_city_err);
        city_line = view.findViewById(R.id.city_line);

        fl_city = view.findViewById(R.id.fl_city);
        et_city = view.findViewById(R.id.et_city);
        ll_commune = view.findViewById(R.id.ll_commune);
        tv_commune = view.findViewById(R.id.tv_commune);
        tv_commune_err = view.findViewById(R.id.tv_commune_err);
        commune_line = view.findViewById(R.id.commune_line);
        fl_commune = view.findViewById(R.id.fl_commune);
        ll_address = view.findViewById(R.id.ll_address);
        tv_address = view.findViewById(R.id.tv_address);
        tv_address_err = view.findViewById(R.id.tv_address_err);
        address_line = view.findViewById(R.id.address_line);
        ll_marital_status = view.findViewById(R.id.ll_marital_status);
        tv_marital_status = view.findViewById(R.id.tv_marital_status);
        tv_marital_status_err = view.findViewById(R.id.tv_marital_status_err);
        status_line = view.findViewById(R.id.status_line);
        fl_marital_status = view.findViewById(R.id.fl_marital_status);
        ll_emergency_name = view.findViewById(R.id.ll_emergency_name);
        tv_emergency_name = view.findViewById(R.id.tv_emergency_name);
        emergency = view.findViewById(R.id.emergency_con);
        tv_add_tiem = view.findViewById(R.id.tv_add_tiem);


        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        emergency.setLayoutManager(layoutManager);
        adapter = new EmergencyAdapter(emergencyCon);
        emergency.setAdapter(adapter);
        adapter.setEmergencyListener(new EmergencyAdapter.EmergencyListener() {
            @Override
            public void relationShips(int id) {
                closeInput();
                showProfession(getSingleSelectList(), "Relación o parentesco", 5, id);
            }

            @Override
            public void updataName(int id, String name) {
                for (int i = 0; i < emergencyCon.size(); i++) {
                    if (emergencyCon.get(i).getId() == id) {
                        emergencyCon.get(i).setName(name);
                    }
                }

            }

            @Override
            public void telephoneNumber(int id, String phone) {
                for (int i = 0; i < emergencyCon.size(); i++) {
                    if (i == id) {
                        emergencyCon.get(i).setPhoneNumber(phone);
                    }
                }
            }

            @Override
            public void removeEmergencyItem(int position) {
                for (int i = 0; i < emergencyCon.size(); i++) {
                    if (position == i) {
                        emergencyCon.remove(i);
                        adapter.notifyDataSetChanged();
                    }
                }
            }

        });

        et_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_adddizhiss_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_adddizhiss_click");
            }
        });
        noChinese(et_address);
        tv_to.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_commit_family_info_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_commit_family_info_click");
                String city = et_city.getText().toString();
                String province = et_region.getText().toString();
                String colony = et_commune.getText().toString();
                String postalCode = et_postal_code.getText().toString();
                String currentAddress = et_address.getText().toString();
                String maritalStatus = et_marital_status.getText().toString();
                hideTipsErr();
//                familyInfoConfigBean
                if (familyInfoConfigBean == null) {
                    return;
                }

                if (TextUtils.equals(familyInfoConfigBean.getCounty(),"1")) {
                    if (TextUtils.isEmpty(province)) {
                        linearLayoutOffsetY(parent, ll_region);
                        tv_region_err.setVisibility(View.VISIBLE);
                        region_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }

                    if (TextUtils.isEmpty(colony)) {
                        linearLayoutOffsetY(parent, ll_commune);
                        tv_commune_err.setVisibility(View.VISIBLE);
                        commune_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }

                if (TextUtils.equals(familyInfoConfigBean.getConstituency(), "1")) {
                    if (TextUtils.isEmpty(city)) {
                        linearLayoutOffsetY(parent, ll_city);
                        tv_city_err.setVisibility(View.VISIBLE);
                        city_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }

                if (TextUtils.equals(familyInfoConfigBean.getPostalCode(),"1")) {
                    if (hasChinese(postalCode)) {
                        linearLayoutOffsetY(parent, ll_postal_code);
                        tv_postal_code_err.setVisibility(View.VISIBLE);
                        postal_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }

                if (TextUtils.equals(familyInfoConfigBean.getCurrentAddress(), "1")) {
                    if (hasChinese(currentAddress)) {
                        linearLayoutOffsetY(parent, ll_address);
                        tv_address_err.setVisibility(View.VISIBLE);
                        address_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }

                if (TextUtils.equals(familyInfoConfigBean.getMaritalStatus(), "1") ||
                        TextUtils.equals(familyInfoConfigBean.getMaritalStatus(), "2")) {
                    if (hasChinese(maritalStatus)) {
                        linearLayoutOffsetY(parent, ll_marital_status);
                        tv_marital_status_err.setVisibility(View.VISIBLE);
                        status_line.setBackgroundColor(Color.parseColor("#4DFF3737"));
                        return;
                    }
                }


                for (int i = 0; i < emergencyCon.size(); i++){
                    emergencyCon.get(i).setNumErr(null);
                    emergencyCon.get(i).setShowRelationShipErr(false);
                    emergencyCon.get(i).setShowNameErr(false);
                }
                boolean isNamePass = true;
                for (int i = 0; i < emergencyCon.size(); i++) {
                    if (TextUtils.isEmpty(emergencyCon.get(i).getName())){
                        emergencyCon.get(i).setShowNameErr(true);
                        isNamePass = false;
                    }
                }
                if (!isNamePass) {
                    adapter.notifyDataSetChanged();
                    return;
                }
                adapter.notifyDataSetChanged();

                boolean isPhonePass = true;
                List<String> phoneList = new ArrayList<String>();
                for (int i = 0; i < emergencyCon.size(); i++) {
                    if (TextUtils.isEmpty(emergencyCon.get(i).getPhoneNumber()) ) {
                        emergencyCon.get(i).setNumErr("Por favor, complete el número");
                        isPhonePass = false;
                    } else {
                        if(!emergencyCon.get(i).getPhoneNumber().startsWith("9")) {
                            emergencyCon.get(i).setNumErr("Formato erróneo");
                            isPhonePass = false;
                        } else {
                            if (emergencyCon.get(i).getPhoneNumber().length() < 9) {
                                emergencyCon.get(i).setNumErr("Formato erróneo");
                                isPhonePass = false;
                            } else {
                                if ( isAdded() ){
                                    SharedPreferences getMobile = requireActivity().getSharedPreferences("personalMobile", Context.MODE_PRIVATE);
                                    String mobile = getMobile.getString("mobile", "");
                                    if (!TextUtils.isEmpty(mobile) && mobile.equals(emergencyCon.get(i).getPhoneNumber())) {
                                        emergencyCon.get(i).setNumErr("El número de contacto de emergencia no puede ser el mismo que el número de registro.");
                                        isPhonePass = false;
                                    } else {
                                        phoneList.add(emergencyCon.get(i).getPhoneNumber());
                                    }
                                }
                            }
                        }
                    }

                }
                if (isPhonePass) {
                    Set<String> uniquePhoneSet = new HashSet<>(phoneList);
                    boolean isUniquePhone = uniquePhoneSet.size() == phoneList.size();
                    if (!isUniquePhone) {
                        for (int i = 0; i < emergencyCon.size(); i++) {
                            emergencyCon.get(i).setNumErr("Los números de contacto de emergencia no pueden ser los mismos");
                        }
                        adapter.notifyDataSetChanged();
                        return;
                    } else {

                        adapter.notifyDataSetChanged();
                    }
                } else {
                    adapter.notifyDataSetChanged();
                    return;
                }
                boolean isReleationShipPass = true;
                for (int i = 0; i < emergencyCon.size(); i++){
                    if (TextUtils.isEmpty(emergencyCon.get(i).getRelationship())) {
                        emergencyCon.get(i).setShowRelationShipErr(true);
                        isReleationShipPass = false;
                    }
                }
                if (!isReleationShipPass) {
                    return;
                } else {
                    adapter.notifyDataSetChanged();
                }

                userupdateList();
            }
        });
        fl_region.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_shuru_reshengfn_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_shuru_reshengfn_click");
                closeInput();
                tv_region_err.setVisibility(View.GONE);
                region_line.setBackgroundColor(Color.parseColor("#33000000"));
                showProfession(getProvinceList(), tv_region.getText().toString(), 1, 0);
            }
        });
        fl_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_chengs_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_chengs_click");
                closeInput();

                tv_city_err.setVisibility(View.GONE);
                city_line.setBackgroundColor(Color.parseColor("#33000000"));
                if ( !et_region.getText().equals("") ){
                    showProfession(getCityList(), tv_city.getText().toString(), 2, 0);
                }

            }
        });
        fl_commune.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_disdiqurict_click");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_disdiqurict_click");
                closeInput();
                tv_commune_err.setVisibility(View.GONE);
                commune_line.setBackgroundColor(Color.parseColor("#33000000"));
                if ( !et_region.getText().equals("") && !et_city.getText().equals("") ){
                    showProfession(getDistrictList(), tv_commune.getText().toString(), 3, 0);
                }
            }
        });
        fl_marital_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_marital_1_status_click_yes");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_marital_1_status_click_yes");
                closeInput();
                tv_marital_status_err.setVisibility(View.GONE);
                status_line.setBackgroundColor(Color.parseColor("#33000000"));
                showProfession(getMaritalStatusSingleSelectList(),
                        tv_marital_status.getText().toString(), 4, 0);
            }
        });
        tv_add_tiem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_marital_2_status_click_yes");
                Log.e("PointMgr.INSTANCE.sendEvent", "cl_marital_2_status_click_yes");
                addEmergency();
            }
        });
    }

    private void hideTipsErr() {
        tv_region_err.setVisibility(View.GONE);
        tv_city_err.setVisibility(View.GONE);
        tv_commune_err.setVisibility(View.GONE);
        tv_postal_code_err.setVisibility(View.GONE);
        tv_address_err.setVisibility(View.GONE);
        tv_marital_status_err.setVisibility(View.GONE);
        postal_line.setBackgroundColor(Color.parseColor("#33000000"));
        region_line.setBackgroundColor(Color.parseColor("#33000000"));
        city_line.setBackgroundColor(Color.parseColor("#33000000"));
        commune_line.setBackgroundColor(Color.parseColor("#33000000"));
        address_line.setBackgroundColor(Color.parseColor("#33000000"));
        status_line.setBackgroundColor(Color.parseColor("#33000000"));
    }

    @Override
    public void initData() {
        getAssetsValue();
        for (int i = 0; i <2 ; i++) {
            addEmergency();
        }
        getZiLiPageUserFullInfo();
        getZiLiPageUserInfoConfig();

    }

    private void userupdateList() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("province", et_region.getText().toString());
        params.put("city", et_city.getText().toString());
        params.put("colony", et_commune.getText().toString());
        if (ll_postal_code.getVisibility() == View.VISIBLE) {
            params.put("postalCode", et_postal_code.getText().toString());
        }
        if (ll_address.getVisibility() == View.VISIBLE) {
            params.put("currentAddress", et_address.getText().toString());
        }
        params.put("maritalStatus", marital_status);

        List<Map<String, String>> updateList = new ArrayList<>();
        for (int i = 0; i < emergencyCon.size(); i++) {
            EmergencyBean conBean = emergencyCon.get(i);
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("name", conBean.getName());
            itemMap.put("mobile", conBean.getPhoneNumber());
            itemMap.put("relation", conBean.getRelationship());
            updateList.add(itemMap);
        }
        params.put("updateList", updateList);
        LogUtils.logE("3 page params>" + GsonUtils.INSTANCE.getGson().toJson(params));
        fatherActivity.showLoading();
        kycModle.updateZiLiPageList(params, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    if (kycModle.getStringToJson(jsonObject, "code").equals("0000")) {
                        PointMgr.INSTANCE.sendEvent("cl_complete_family_info");
                        Log.e("PointMgr.INSTANCE.sendEvent", "cl_complete_family_info");
                        CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                        if (capitalInforActivity != null) {
                            capitalInforActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    capitalInforActivity.onTabSelected(capitalInforActivity.TAG_INDEX_FOUR);
                                    capitalInforActivity.setTabIndex(3);
                                }
                            });
                        }
                    } else {
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtils.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                fatherActivity.hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                T.showShort((CapitalInforActivity) getActivity(), e);
                CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                if (capitalInforActivity != null) {
                    capitalInforActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            capitalInforActivity.onTabSelected(capitalInforActivity.TAG_INDEX_THREE);
                            capitalInforActivity.setTabIndex(3);
                        }
                    });
                }
                fatherActivity.hideLoading();
            }
        });
    }

    private void getAssetsValue() {
        String value = kycModle.getAssetsValue(activity);
        if (!TextUtils.isEmpty(value)) {
            addressBean = new Gson().fromJson(value, ZiLiPageAddressBean.class);
            areaList = addressBean.getAreaListInfo();
            Log.e("----areaList", String.valueOf(areaList));
        }
    }


    private void addEmergency() {
        EmergencyBean emergencyBean = new EmergencyBean();
        emergencyBean.setName("");
        emergencyBean.setRelationship("");
        emergencyBean.setPhoneNumber("");
        emergencyBean.setWrite(false);
        emergencyCon.add(emergencyBean);
        emergencyBean.setId(emergencyCon.size() - 1);
        adapter.notifyDataSetChanged();
    }

    // Padres
    // Cónyuge
    // Hijos
    // Hermanos
    // Amigo
    // Colega"

    public List<SingleSelectBean> getSingleSelectList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        LinkedList<String> stringList = new LinkedList<>();
        stringList.add("Padres");
        stringList.add("Cónyuge");
        stringList.add("Hijos");
        stringList.add("Hermanos");
        stringList.add("Amigo");
        stringList.add("Colega");
        for (int i = 0; i < stringList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBean.setText(stringList.get(i));
            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    public List<SingleSelectBean> getMaritalStatusSingleSelectList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        LinkedList<String> stringList = new LinkedList<>();
        stringList.add("Soltero(a)");
        stringList.add("Casado(a)");
        stringList.add("Viudo(a)");
        stringList.add("Divorciado(a)");
        stringList.add("Pareja de hecho(a)");
        stringList.add("Separado(a)");
        for (int i = 0; i < stringList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBean.setText(stringList.get(i));
            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    public List<SingleSelectBean> getProvinceList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        Log.e("----areaList", areaList.toString());
        for (int i = 0; i < areaList.size(); i++) {

            SingleSelectBean singleSelectBean = new SingleSelectBean();
            String s = areaList.get(i).getProvince();
            singleSelectBean.setText(s);
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBeans.add(singleSelectBean);

        }
        return singleSelectBeans;
    }

    public List<SingleSelectBean> getCityList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        Log.e("----cityList", cityList.toString());
        for (int i = 0; i < cityList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            String s = cityList.get(i).getCity();
            singleSelectBean.setText(s);
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    public List<SingleSelectBean> getDistrictList() {
        List<SingleSelectBean> singleSelectBeans = new ArrayList<>();
        Log.e("----districtList", districtList.toString());
        for (int i = 0; i < districtList.size(); i++) {
            SingleSelectBean singleSelectBean = new SingleSelectBean();
            String s = districtList.get(i).getColony();
            singleSelectBean.setText(s);
            singleSelectBean.setId(i);
            singleSelectBean.setCheck(false);
            singleSelectBeans.add(singleSelectBean);
        }
        return singleSelectBeans;
    }

    public void getZiLiPageCity(ZiLiPageArea area) {
        if (area != null) {
            cityList = area.getCityListInfo();
        }
    }

    public void getZiLiPageDistrict(ZiLiPageCity city) {
        if (city != null) {
            districtList = city.getColonyListInfo();
        }
    }

    private void getZiLiPageUserInfoConfig() {
        Map<String, String> params = new HashMap<String, String>();
        fatherActivity.showLoading();
        kycModle.getZiLiPageUserInfoConfig(params, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    AppKycPersonalInformacionConfig applicationUserInfoConfigBean = new AppKycPersonalInformacionConfig();
                    applicationUserInfoConfigBean.initConfigData(jsonObject.optJSONObject("data"));
                    familyInfoConfigBean =
                            applicationUserInfoConfigBean.getFamilyInfoConfig();
                    CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                    if (capitalInforActivity != null){
                        capitalInforActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (familyInfoConfigBean != null) {
                                    Log.e("----familyInfoConfigBean", GsonUtils.INSTANCE.getGson().toJson(familyInfoConfigBean));
                                    isOhterShowView(familyInfoConfigBean.getCounty(), ll_region, tv_region, "Región");
                                    isOhterShowView(familyInfoConfigBean.getConstituency(), ll_city, tv_city, "Ciudad");
                                    isOhterShowView(familyInfoConfigBean.getCounty(), ll_commune, tv_commune, "Comuna");
                                    isOhterShowView(familyInfoConfigBean.getMaritalStatus(), ll_marital_status, tv_marital_status, "Estado civil");
                                    isOhterShowView(familyInfoConfigBean.getPostalCode(), ll_postal_code, tv_postal_code, "Código postal");
                                    isOhterShowView(familyInfoConfigBean.getCurrentAddress(), ll_address, tv_address, "Dirección de domicilioDirección de domicilio");
                                }
                            }
                        });
                    }

//                    if (TextUtils.equals(familyInfoConfigBean.getPostalCode(), "1")) {
//                        ll_postal_code.setVisibility(View.VISIBLE);
//                    } else {
//                        ll_postal_code.setVisibility(View.GONE);
//                    }
//                    if (TextUtils.equals(familyInfoConfigBean.getCurrentAddress(), "1")) {
//                        ll_address.setVisibility(View.VISIBLE);
//                    } else {
//                        ll_address.setVisibility(View.GONE);
//                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                fatherActivity.hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                fatherActivity.hideLoading();
            }
        });
    }

    private void getZiLiPageUserFullInfo() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("custId", ZiLiPageUserDataBean.getInstance().getCustomerId());
        fatherActivity.showLoading();
        kycModle.getZiLiPageUserFullInfo(params, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    String coustList = jsonObject.optString("contactInfo");
                    GetFullUserBean fullUserBean = new Gson().fromJson(data, GetFullUserBean.class);
                    List<EmergencyBean> cList = new ArrayList<>();
                    for (int i = 0; i < fullUserBean.getData().getInfo().size(); i++) {
                        EmergencyBean item = new EmergencyBean();
                        item.setName(fullUserBean.getData().getInfo().get(i).getName());
                        item.setPhoneNumber(fullUserBean.getData().getInfo().get(i).getMobile());
                        item.setRelationship(fullUserBean.getData().getInfo().get(i).getRelation());
                        cList.add(item);
                    }
                    LogUtils.logE("cantacts 2>" + GsonUtils.INSTANCE.getGson().toJson(cList));
                    if (kycModle.isSuccess(jsonObject)) {
                        showHistoryData(jsonObject.optJSONObject("data"), cList);
                    } else {
                        T.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                fatherActivity.hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                fatherActivity.hideLoading();
            }
        });
    }

    String marital_status;

    private void showHistoryData(JSONObject jsonObject, List<EmergencyBean> conList) {
        Log.e("----","getValue");

        getAssetsValue();
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String city = kycModle.getStringToJson(jsonObject, "city");
                String colony = kycModle.getStringToJson(jsonObject, "colony");
                String currentAddress = kycModle.getStringToJson(jsonObject, "currentAddress");
                if (!TextUtils.isEmpty(city) && !city.equals("null")) {
                    et_city.setText(city);
                }
                Log.e("----",city);
                if (!TextUtils.isEmpty(colony) && !colony.equals("null")) {
                    et_commune.setText(colony);
                }
                Log.e("----",colony);
                if (!TextUtils.isEmpty(currentAddress) && !currentAddress.equals("null")) {
                    et_address.setText(currentAddress);
                }
                Log.e("----",currentAddress);
                marital_status = kycModle.getStringToJson(jsonObject, "maritalStatus");
                if (!TextUtils.isEmpty(marital_status) && !marital_status.equals("null")) {
                    if (marital_status.equals("A")) {
                        et_marital_status.setText("Soltero(a)");
                    } else if (marital_status.equals("B")) {
                        et_marital_status.setText("Casado(a)");
                    } else if (marital_status.equals("C")) {
                        et_marital_status.setText("Viudo(a)");
                    } else if (marital_status.equals("D")) {
                        et_marital_status.setText("Separado(a)");
                    } else if (marital_status.equals("E")) {
                        et_marital_status.setText("Divorciado(a)");
                    } else {
                        et_marital_status.setText("Pareja de hecho(a)");
                    }
                }

                String province = kycModle.getStringToJson(jsonObject, "province");
                if (!TextUtils.isEmpty(province) && !province.equals("null")) {
                    Log.e("----01areaList",areaList.toString());
                    for (int i = 0; i < areaList.size(); i++) {
                        if (TextUtils.equals(province, areaList.get(i).getProvince())) {
                            getZiLiPageCity(areaList.get(i));
                        }
                    }
                    Log.e("----01cityList",cityList.toString());
                    for (int i = 0; i < cityList.size(); i++) {
                        if (TextUtils.equals(et_city.getText().toString(), cityList.get(i).getCity())) {
                            getZiLiPageDistrict(cityList.get(i));
                        }
                    }
                    Log.e("----01province",province);
                    et_region.setText(province);
                }

                if (conList != null && conList.size() > 0) {
                    emergencyCon.clear();
                    emergencyCon.addAll(conList);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }


    private void showProfession(List<SingleSelectBean> dataList, String title, int key, int id) {
        SingleSelectDialogView dialogView = new SingleSelectDialogView(activity, view, title);

        dialogView.setSingleSelectListener(new SingleSelectDialogView.SingleSelectListener() {
            @Override
            public void checked(SingleSelectBean s) {
                if (key == 1) {
                    et_region.setText(s.getText());
                    et_city.setText(null);
                    et_commune.setText(null);
                    for (int i = 0; i < areaList.size(); i++) {
                        if (TextUtils.equals(s.getText(), areaList.get(i).getProvince())) {
                            getZiLiPageCity(areaList.get(i));
                        }
                    }
                } else if (key == 2) {
                    et_city.setText(s.getText());
                    et_commune.setText(null);
                    for (int i = 0; i < cityList.size(); i++) {
                        if (TextUtils.equals(s.getText(), cityList.get(i).getCity())) {
                            getZiLiPageDistrict(cityList.get(i));
                        }
                    }
                } else if (key == 3) {
                    et_commune.setText(s.getText());
                } else if (key == 4) {
                    et_marital_status.setText(s.getText());
                    if (et_marital_status.getText().toString().equals("Soltero(a)")) {
                        marital_status = "A";
                    } else if (et_marital_status.getText().toString().equals("Casado(a)")) {
                        marital_status = "B";
                    } else if (et_marital_status.getText().toString().equals("Viudo(a)")) {
                        marital_status = "C";
                    } else if (et_marital_status.getText().toString().equals("Separado(a)")) {
                        marital_status = "D";
                    } else if (et_marital_status.getText().toString().equals("Divorciado(a)")) {
                        marital_status = "E";
                    } else {
                        marital_status = "F";
                    }
                } else if (key == 5) {
                    for (int i = 0; i < emergencyCon.size(); i++) {
                        EmergencyBean contactBean = emergencyCon.get(i);
                        if (i == id) {
                            emergencyCon.get(i).setRelationship(s.getText());
                            emergencyCon.get(i).setShowRelationShipErr(false);
                            if (!TextUtils.isEmpty(s.getText())) {
                                if (s.getText().equals("Padres")) {
                                    emergencyCon.get(i).setRelationship("A");
                                } else if (s.getText().equals("Cónyuge")) {
                                    emergencyCon.get(i).setRelationship("B");
                                } else if (s.getText().equals("Hijos")) {
                                    emergencyCon.get(i).setRelationship("C");
                                } else if (s.getText().equals("Hermanos")) {
                                    emergencyCon.get(i).setRelationship("D");
                                } else if (s.getText().equals("Amigo")) {
                                    emergencyCon.get(i).setRelationship("E");
                                } else {
                                    emergencyCon.get(i).setRelationship("F");
                                }
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
                dialogView.dismiss();
                activityListener.sendValue("beLight");
            }
            @Override
            public void closeDialog(){
                dialogView.dismiss();
                activityListener.sendValue("beLight");
            }
        });
        dialogView.showSingleSelectDialog(activity, dataList);
        activityListener.sendValue("beDark");
    }

    private void closeInput() {
        if (getActivity().getCurrentFocus() == null) {
            return;
        }
        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    public void onPause(){
        super.onPause();
        fatherActivity.hideLoading();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        PointMgr.INSTANCE.sendEvent("cl_family_info_exit");
        Log.e("PointMgr.INSTANCE.sendEvent", "cl_family_info_exit");
    }

    private void noChinese(EditText editText){
        editText.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence charSequence, int start, int end, Spanned spanned, int i2, int i3) {
                for (int i = start; i < end; i++) {
                    if (Character.UnicodeBlock.of(charSequence.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }

}
